package com.shoulder.boot.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoulder.boot.system.dto.TenantAddDTO;
import com.shoulder.boot.system.dto.TenantModDTO;
import com.shoulder.boot.system.dto.TenantQueryDTO;
import com.shoulder.boot.system.service.TenantService;
import com.shoulder.boot.system.vo.TenantVo;
import com.shoulder.core.api.ApiResult;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.page.PageSchema;
import com.shoulder.log.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户信息管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin/api/v1/system/tenant")
public class TenantController {

    private final TenantService tenantService;

    /**
     * 租户状态切换
     *
     * @param id 租户id
     * @return ApiResult<Object>
     */
    @PutMapping("/switch/{id}")
    @LogAnnotation(type = "租户状态切换")
    public ApiResult<Object> change(@PathVariable String id) {
        this.tenantService.switchStatus(id);
        return ApiResult.ok();
    }

    /**
     * 添加租户信息
     *
     * @param addDTO 租户添加请求体
     * @return ApiResult<Object>
     * @author 59676880@qq.com
     */
    @PostMapping
    @LogAnnotation(type = "添加租户信息")
    public ApiResult<Object> add(@RequestBody @Validated TenantAddDTO addDTO) {
        this.tenantService.add(addDTO);
        return ApiResult.ok();
    }

    /**
     * 修改租户信息
     *
     * @param modDTO 租户编辑请求体
     * @return ApiResult<Object>
     * @author 59676880@qq.com
     */
    @PutMapping
    @LogAnnotation(type = "修改租户信息")
    public ApiResult<Object> mod(@RequestBody @Validated TenantModDTO modDTO) {
        this.tenantService.mod(modDTO);
        return ApiResult.ok();
    }

    /**
     * 查询租户信息分页数据
     *
     * @param queryDTO 租户查询请求体
     * @return ApiResult<PageSchema < TenantVo>>
     * @author 59676880@qq.com
     */
    @LogAnnotation(type = "租户信息分页查询", record = false)
    @PostMapping("/page")
    public ApiResult<PageSchema<TenantVo>> pageQuery(@RequestBody TenantQueryDTO queryDTO) {
        Page<TenantVo> page = this.tenantService.pageQuery(queryDTO);
        return ApiResult.ok(new PageSchema<>(page.getTotal(), page.getRecords()));
    }

    /**
     * 查询租户信息列表数据
     *
     * @param queryDTO 租户查询请求体
     * @return ApiResult<List < Tenant>>
     * @author 59676880@qq.com
     */
    @LogAnnotation(type = "租户信息列表查询", record = false)
    @PostMapping("/list")
    public ApiResult<List<TenantVo>> list(@RequestBody TenantQueryDTO queryDTO) {
        List<TenantVo> list = this.tenantService.listQuery(queryDTO);
        return ApiResult.ok(list);
    }

    /**
     * 批量删除租户信息
     *
     * @param baseIds 租户id集合
     * @return ApiResult<Object>
     * @author 59676880@qq.com
     */
    @PostMapping("/rm")
    @LogAnnotation(type = "批量删除租户信息")
    public ApiResult<Object> delete(@RequestBody @Validated BaseIds<String> baseIds) {
        this.tenantService.batchDel(baseIds);
        return ApiResult.ok();
    }


    /**
     * 根据id删除租户信息
     *
     * @param id 租户信息id
     * @return ApiResult<Object>
     * @author 59676880@qq.com
     */
    @DeleteMapping("/{id}")
    @LogAnnotation(type = "删除租户信息")
    public ApiResult<Object> del(@PathVariable String id) {
        this.tenantService.del(id);
        return ApiResult.ok();
    }


    /**
     * 批量查询租户信息
     *
     * @param baseIds 租户id集合
     * @return ApiResult<List < Tenant>>
     * @author 59676880@qq.com
     */
    @PostMapping("/look")
    @LogAnnotation(type = "批量查询租户信息", record = false)
    public ApiResult<List<TenantVo>> looks(@RequestBody BaseIds<String> baseIds) {
        List<TenantVo> list = this.tenantService.looks(baseIds);
        return ApiResult.ok(list);
    }

    /**
     * 根据id获取租户信息
     *
     * @param id 租户信息id
     * @return ApiResult<Tenant>
     * @author 59676880@qq.com
     */
    @GetMapping("/{id}")
    @LogAnnotation(type = "根据id查询租户信息", record = false)
    public ApiResult<TenantVo> look(@PathVariable String id) {
        TenantVo look = this.tenantService.look(id);
        return ApiResult.ok(look);
    }
}
